package com.yang.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yang.mall.dto.CreateProductDTO;
import com.yang.mall.dto.ProductDTO;
import com.yang.mall.dto.ProductUpDownDTO;
import com.yang.mall.entity.Product;
import com.yang.mall.entity.QueryRequest;
import com.yang.mall.mapper.ProductMapper;
import com.yang.mall.service.IProductService;
import com.yang.mall.tools.BeanUtils;
import com.yang.mall.vo.ProductDetailVO;
import com.yang.mall.vo.ProductVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商品库表 服务实现类
 * </p>
 *
 * @author kadorlin
 * @since 2022-10-21
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    /**
     * 创建商品
     * @param createProductDTO
     * @author:kadorlin
     * @date:2022/10/21 13:41
     * @return:void
     **/
    @Override
    public void createOrUpdate(CreateProductDTO createProductDTO) {
        Product product = BeanUtils.copyBeanProp(createProductDTO, Product.class);
        this.saveOrUpdate(product);
    }



    /**
     * 分页查询商品
     *
     * @param productDTO
     * @author:kadorlin
     * @date:2022/10/21 13:41
     * @return:com.baomidou.mybatisplus.core.metadata.IPage<com.yang.mall.vo.ProductVO>
     **/
    @Override
    public IPage<ProductVO> findPage(ProductDTO productDTO) {
        Page<ProductVO> page = new Page<>(productDTO.getPageNum(), productDTO.getPageSize());
        return baseMapper.findList(page, productDTO);
    }

    /**
     * 删除商品
     * @param productIds
     * @author:kadorlin
     * @date:2022/10/21 13:41
     * @return:void
     **/
    @Override
    public void deleteProductByIds(List<Long> productIds) {
        this.removeByIds(productIds);
    }

    /**
     * 批量查询商品
     * @param productIds
     * @author:kadorlin
     * @date:2022/10/24 10:31
     * @return:java.util.List<com.yang.mall.entity.Product>
     **/
    @Override
    public List<Product> getProductByIds(List<Long> productIds) {
        QueryWrapper<Product> qw = new QueryWrapper<>();
        qw.lambda().in(Product::getProductId, productIds);
        return this.list(qw);
    }


    /**
     * 商品详情
     * @param productId
     * @author:kadorlin
     * @date:2022/10/21 14:15
     * @return:com.yang.mall.vo.ProductDetailVO
     **/
    @Override
    public ProductDetailVO detail(Long productId) {
        return baseMapper.getProductDetailVO(productId);
    }

    /**
     * 根据条码查询商品详情
     * @param barCode
     * @author:kadorlin
     * @date:2022/10/24 09:54
     * @return:com.yang.mall.vo.ProductDetailVO
     **/
    @Override
    public ProductDetailVO detailByBarCode(String barCode) {
        QueryWrapper<Product> qw = new QueryWrapper<>();
        qw.lambda().eq(Product::getBarCode, barCode);
        Product one = this.getOne(qw);
        return BeanUtils.copyBeanProp(one,ProductDetailVO.class);
    }

    /**
     * @param productDTO
     * @author:kadorlin
     * @date: 17:03
     * @return:java.util.List<com.yang.mall.vo.ProductVO>
     **/
    @Override
    public List<ProductVO> queryList(ProductDTO productDTO) {
        QueryWrapper<Product> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(Objects.nonNull(productDTO.getSortId()),Product::getSortId, productDTO.getSortId())
                .like(StringUtils.isNotEmpty(productDTO.getProductName()),Product::getProductName, productDTO.getProductName());
        List<Product> list = this.list(qw);
        return BeanUtils.copyBeanProp(list,ProductVO.class);
    }
}
